const MyPromise = require("../template.js")

/** 十一、Promise.race 测试 */

// 测试 1: 第一个 Promise 先完成（成功）
function testRaceFirstWins() {
  const p1 = new MyPromise((resolve) => setTimeout(() => resolve(1), 50))
  const p2 = new MyPromise((resolve) => setTimeout(() => resolve(2), 100))

  MyPromise.race([p1, p2]).then((value) =>
    console.log("11.1 Race first wins:", value === 1)
  )
}

// 测试 2: 第二个 Promise 先完成
function testRaceSecondWins() {
  const p1 = new MyPromise((resolve) => setTimeout(() => resolve(1), 100))
  const p2 = new MyPromise((resolve) => setTimeout(() => resolve(2), 50))

  MyPromise.race([p1, p2]).then((value) =>
    console.log("11.2 Race second wins:", value === 2)
  )
}

// 测试 3: 第一个失败
function testRaceFirstRejects() {
  const p1 = new MyPromise((_, reject) => setTimeout(() => reject("Fast Error"), 50))
  const p2 = new MyPromise((resolve) => setTimeout(() => resolve(2), 100))

  MyPromise.race([p1, p2]).then(
    (value) => console.log("11.3 Race first rejects:", false),
    (reason) => console.log("11.3 Race first rejects:", reason === "Fast Error")
  )
}

// 测试 4: 包含普通值（立即完成）
function testRaceWithNonPromise() {
  const p1 = 1 // 普通值
  const p2 = new MyPromise((resolve) => setTimeout(() => resolve(2), 50))

  MyPromise.race([p1, p2]).then((value) =>
    console.log("11.4 Race with non-promise:", value === 1)
  )
}

// 测试 5: 所有都是立即 resolve
function testRaceAllImmediate() {
  const p1 = MyPromise.resolve(1)
  const p2 = MyPromise.resolve(2)
  const p3 = MyPromise.resolve(3)

  MyPromise.race([p1, p2, p3]).then((value) =>
    console.log("11.5 Race all immediate:", value === 1)
  )
}

// 测试 6: 所有都是立即 reject
function testRaceAllReject() {
  const p1 = MyPromise.reject("Error 1")
  const p2 = MyPromise.reject("Error 2")

  MyPromise.race([p1, p2]).then(
    (value) => console.log("11.6 Race all reject:", false),
    (reason) => console.log("11.6 Race all reject:", reason === "Error 1")
  )
}

// 测试 7: 空数组（永远 pending）
function testRaceEmpty() {
  const racePromise = MyPromise.race([])
  setTimeout(() => {
    console.log("11.7 Race empty:", racePromise instanceof MyPromise)
  }, 100)
}

// 测试 8: 第一个是 rejected Promise
function testRaceFirstIsRejected() {
  const p1 = MyPromise.reject("First Error")
  const p2 = MyPromise.resolve(2)

  MyPromise.race([p1, p2]).then(
    (value) => console.log("11.8 Race first is rejected:", false),
    (reason) => console.log("11.8 Race first is rejected:", reason === "First Error")
  )
}

// 测试 9: 最快的是成功的
function testRaceFastestSuccess() {
  const p1 = new MyPromise((_, reject) => setTimeout(() => reject("Slow Error"), 100))
  const p2 = new MyPromise((resolve) => setTimeout(() => resolve(2), 50))
  const p3 = new MyPromise((resolve) => setTimeout(() => resolve(3), 150))

  MyPromise.race([p1, p2, p3]).then((value) =>
    console.log("11.9 Race fastest success:", value === 2)
  )
}

// 测试 10: 包含 thenable 对象
function testRaceWithThenable() {
  const thenable = {
    then: (resolve) => {
      setTimeout(() => resolve("thenable"), 30)
    }
  }
  const p1 = new MyPromise((resolve) => setTimeout(() => resolve(1), 100))

  MyPromise.race([thenable, p1]).then((value) =>
    console.log("11.10 Race with thenable:", value === "thenable")
  )
}

testRaceFirstWins()
testRaceSecondWins()
testRaceFirstRejects()
testRaceWithNonPromise()
testRaceAllImmediate()
testRaceAllReject()
testRaceEmpty()
testRaceFirstIsRejected()
testRaceFastestSuccess()
testRaceWithThenable()
